package 面试题06_从尾到头打印链表.栈实现;

import java.util.Stack;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 10:34 2021/2/5
 * @Description：
 */
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

class Solution {
    public int[] reversePrint(ListNode head) {

        Stack<Integer> stack = new Stack<Integer>();

        ListNode currentNode = head;
        while (currentNode != null) {
            stack.push(currentNode.val);

            currentNode = currentNode.next;

        }

        // 统计节点的数量，随着栈的弹出，栈的大小在发生变化
        int length = stack.size();

        int[] print = new int[length];

        for (int i = 0; i < length; i++) {
            print[i] = stack.pop();
        }

        return print;
    }
}
